package com.CNMedical.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.diboot.core.binding.Binder;
import com.diboot.core.service.impl.BaseServiceImpl;
import com.CNMedical.common.core.domain.R;
import com.CNMedical.common.utils.DateUtils;
import com.CNMedical.system.domain.DTO.AppletExamScoreDTO;
import com.CNMedical.system.domain.entity.AppletQuestion;
import com.CNMedical.system.domain.vo.AppletExamScoreVO;
import com.CNMedical.system.domain.vo.AppletQuestionVO;
import com.CNMedical.system.service.IAppletQuestionAnswerService;
import com.CNMedical.system.service.IAppletQuestionService;
import com.CNMedical.system.service.IAppletUserService;
import org.springframework.stereotype.Service;
import com.CNMedical.system.mapper.AppletExamScoreMapper;
import com.CNMedical.system.domain.entity.AppletExamScore;
import com.CNMedical.system.service.IAppletExamScoreService;

import javax.annotation.Resource;

/**
 * 考试成绩Service业务层处理
 *
 * @author tang
 * @date 2023-03-28
 */
@Service
public class AppletExamScoreServiceImpl extends BaseServiceImpl<AppletExamScoreMapper, AppletExamScore> implements IAppletExamScoreService {
    @Resource
    private AppletExamScoreMapper appletExamScoreMapper;

    @Resource
    private IAppletQuestionAnswerService appletQuestionAnswerService;

    @Resource
    private IAppletQuestionService appletQuestionService;

    @Resource
    private IAppletUserService appletUserService;

    @Override
    public Boolean isExam(String knowledgeId, String userId) {
        return lambdaQuery().eq(AppletExamScore::getKnowledgeId, knowledgeId)
                .eq(AppletExamScore::getUserId, userId).exists();
    }

    @Override
    public List<AppletExamScoreVO> getFinishExamList(String userId) {
        List<AppletExamScoreVO> appletExamScoreVOS = getViewObjectList(Wrappers.lambdaQuery(AppletExamScore.class)
                        .eq(AppletExamScore::getUserId, userId),
                null,
                AppletExamScoreVO.class);
        for (AppletExamScoreVO appletExamScoreVO : appletExamScoreVOS) {
            appletExamScoreVO.setRank(lambdaQuery().gt(AppletExamScore::getScore, appletExamScoreVO.getScore())
                    .eq(AppletExamScore::getKnowledgeId, appletExamScoreVO.getKnowledgeId())
                    .ne(AppletExamScore::getUserId, appletExamScoreVO.getUserId()).count() + 1);
        }
        return appletExamScoreVOS;
    }

    @Override
    public R countScore(String knowledgeId, String userId) {

        lambdaUpdate().eq(AppletExamScore::getKnowledgeId, knowledgeId).eq(AppletExamScore::getUserId, userId).remove();


        List<AppletQuestionVO> allQuestionByKnowledgeId = appletQuestionService.getAllQuestionByKnowledgeId(knowledgeId);
        final List<Long> questionIdList = allQuestionByKnowledgeId.stream().map(AppletQuestion::getId).collect(Collectors.toList());
        double score = appletQuestionAnswerService.countScore(questionIdList, userId);
        this.save(AppletExamScore.builder().knowledgeId(knowledgeId)
                .userId(userId)
                .score(String.valueOf(score))
                .build());

//        if (!lambdaQuery().eq(AppletExamScore::getKnowledgeId, knowledgeId).eq(AppletExamScore::getUserId, userId).exists()) {
//
//            List<AppletQuestionVO> allQuestionByKnowledgeId = appletQuestionService.getAllQuestionByKnowledgeId(knowledgeId);
//            final List<Long> questionIdList = allQuestionByKnowledgeId.stream().map(AppletQuestion::getId).collect(Collectors.toList());
//            double score = appletQuestionAnswerService.countScore(questionIdList, userId);
//            this.save(AppletExamScore.builder().knowledgeId(knowledgeId)
//                    .userId(userId)
//                    .score(String.valueOf(score))
//                    .build());
//        }
        return R.ok();
    }

    /**
     * 查询考试成绩
     *
     * @param id 考试成绩主键
     * @return 考试成绩
     */
    @Override
    public AppletExamScore selectAppletExamScoreById(Long id) {
        return appletExamScoreMapper.selectAppletExamScoreById(id);
    }

    /**
     * 查询考试成绩列表
     *
     * @param appletExamScore 考试成绩
     * @return 考试成绩
     */
    @Override
    public List<AppletExamScoreVO> selectAppletExamScoreList(AppletExamScoreDTO appletExamScore) {
        List<Long> userIdByUserName = appletUserService.getUserIdByUserName(appletExamScore.getUserName());
        List<AppletExamScore> appletExamScores = appletExamScoreMapper.selectAppletExamScoreList(appletExamScore,userIdByUserName);
        List<AppletExamScoreVO> appletExamScoreVOS = Binder.convertAndBindRelations(appletExamScores, AppletExamScoreVO.class);
        return appletExamScoreVOS;
    }

    /**
     * 新增考试成绩
     *
     * @param appletExamScore 考试成绩
     * @return 结果
     */
    @Override
    public int insertAppletExamScore(AppletExamScore appletExamScore) {
        appletExamScore.setCreateTime(DateUtils.getNowDate());
        return appletExamScoreMapper.insertAppletExamScore(appletExamScore);
    }

    /**
     * 修改考试成绩
     *
     * @param appletExamScore 考试成绩
     * @return 结果
     */
    @Override
    public int updateAppletExamScore(AppletExamScore appletExamScore) {
        appletExamScore.setUpdateTime(DateUtils.getNowDate());
        return appletExamScoreMapper.updateAppletExamScore(appletExamScore);
    }

    /**
     * 批量删除考试成绩
     *
     * @param ids 需要删除的考试成绩主键
     * @return 结果
     */
    @Override
    public int deleteAppletExamScoreByIds(Long[] ids) {
        return appletExamScoreMapper.deleteAppletExamScoreByIds(ids);
    }

    /**
     * 删除考试成绩信息
     *
     * @param id 考试成绩主键
     * @return 结果
     */
    @Override
    public int deleteAppletExamScoreById(Long id) {
        return appletExamScoreMapper.deleteAppletExamScoreById(id);
    }
}
